package controlisolation.lifecyclefsm;

import cakeorcookiexample.CakeOrCookieFSM;
import controlisolation.lifecyclefsm.events.LifeCycleExitForcedEvent;
import controlisolation.lifecyclefsm.events.LifeCycleExitForcedEventListener;


public class Main implements LifeCycleExitForcedEventListener {
    
    private boolean _exitForced=false;

    public static void main(String[] args) throws Exception
    {
        Main main=new Main();
        CakeOrCookieFSM fsm=new CakeOrCookieFSM();
        Thread t=new Thread(fsm);
        fsm.init();
        fsm.getLifeCycleFSM().setDebugMessages(true);
        fsm.getLifeCycleFSM().startRemoteDebugServer(9999);
        fsm.getLifeCycleFSM().addLifeCycleExitForcedEventListener(main);
        t.start();
        
        System.out.println("Running the FSM for 120 seconds.");
        long startT=System.currentTimeMillis();
        
        while((System.currentTimeMillis()-startT<45*60*1000)&&(main.isExitForced()==false))
        {
         try {
            
                Thread.sleep(250);
            } catch (InterruptedException ex) {
               System.out.println("Sleep interrupted");
            }
        }
        fsm.getLifeCycleFSM().stop();
      
    }
    
    @Override
    public void handleLifeCycleExitForcedEvent(LifeCycleExitForcedEvent e) {
        System.out.println("Force exit");
        setExitForced(true);
    }

    public boolean isExitForced() {
        return _exitForced;
    }

    public void setExitForced(boolean exitForced) {
        this._exitForced = exitForced;
    }
}
